package coupon

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"bp/internal/util"
	"context"
	// "database/sql"
)



// GetCouponDistributionPtr 获取优惠券投放记录
func (s *CouponEntityServiceImp) GetCouponDistributionPtr(ctx context.Context, q *sqlc.Queries, distributionID int64) (couponDistributionPtr *sqlc.CouponDistribution, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	couponDistribution, err := q.GetCouponDistribution(ctx, distributionID)
	if err != nil {
		return
	}
	slog.Infof("1 GetCouponDistribution ok")

	couponDistributionPtr = &couponDistribution
	return
}

// ListCouponDistributionPtrsParam 列出优惠券投放参数
type ListCouponDistributionPtrsParam struct {
	CouponID         int64
	Issuers          []string
	DistributeScenes []string
}

// ListCouponDistributionPtrs 列出优惠券投放
func (s *CouponEntityServiceImp) ListCouponDistributionPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponDistributionPtrsParam) (total int64, couponPtrs []*sqlc.CouponDistribution, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCouponDistributionsByCouponIDAndIssuesAndDistributeScenes(ctx, sqlc.CountCouponDistributionsByCouponIDAndIssuesAndDistributeScenesParams{
		Ts:                       cPtr.PagerPtr.Ts,
		CouponID:                 ptr.CouponID,
		FilterAllIssuer:          len(ptr.Issuers) == 0,
		Issuers:                  ptr.Issuers,
		FilterAllDistributeScene: len(ptr.DistributeScenes) == 0,
		DistributeScenes:         ptr.DistributeScenes,
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCouponDistributionsByCouponIDAndIssuesAndDistributeScenes(ctx, sqlc.ListCouponDistributionsByCouponIDAndIssuesAndDistributeScenesParams{
		Ts:                       cPtr.PagerPtr.Ts,
		CouponID:                 ptr.CouponID,
		FilterAllIssuer:          len(ptr.Issuers) == 0,
		Issuers:                  ptr.Issuers,
		FilterAllDistributeScene: len(ptr.DistributeScenes) == 0,
		DistributeScenes:         ptr.DistributeScenes,
		Offset:                   cPtr.PagerPtr.Offset,
		Limit:                    cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCouponDistributionPtrs ok")

	couponPtrs = s.MapCouponDistributionEntityToPtr(rows)
	return
}

// ListCouponDistributionWithStatPtrsParam 列出优惠券投放及其统计参数
type ListCouponDistributionWithStatPtrsParam struct {
	CouponID         int64
	Issuers          []string
	DistributeScenes []string
}

// ListCouponDistributionWithStatPtrs 列出优惠券投放及其统计
func (s *CouponEntityServiceImp) ListCouponDistributionWithStatPtrs(ctx context.Context, q *sqlc.Queries, cPtr *param.ListCommonParam, ptr *ListCouponDistributionWithStatPtrsParam) (total int64, couponPtrs []*sqlc.ListCouponDistributionWithStatsByCouponIDAndIssuesAndDistributeScenesRow, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	total, err = q.CountCouponDistributionsByCouponIDAndIssuesAndDistributeScenes(ctx, sqlc.CountCouponDistributionsByCouponIDAndIssuesAndDistributeScenesParams{
		Ts:                       cPtr.PagerPtr.Ts,
		CouponID:                 ptr.CouponID,
		FilterAllIssuer:          len(ptr.Issuers) == 0,
		Issuers:                  ptr.Issuers,
		FilterAllDistributeScene: len(ptr.DistributeScenes) == 0,
		DistributeScenes:         ptr.DistributeScenes,
	})
	if err != nil {
		return
	}
	if total == 0 {
		return
	}

	rows, err := q.ListCouponDistributionWithStatsByCouponIDAndIssuesAndDistributeScenes(ctx, sqlc.ListCouponDistributionWithStatsByCouponIDAndIssuesAndDistributeScenesParams{
		Ts:                       cPtr.PagerPtr.Ts,
		CouponID:                 ptr.CouponID,
		FilterAllIssuer:          len(ptr.Issuers) == 0,
		Issuers:                  ptr.Issuers,
		FilterAllDistributeScene: len(ptr.DistributeScenes) == 0,
		DistributeScenes:         ptr.DistributeScenes,
		Offset:                   cPtr.PagerPtr.Offset,
		Limit:                    cPtr.PagerPtr.Limit,
	})
	if err != nil {
		return
	}
	slog.Infof("1 ListCouponDistributionWithStatPtrs ok")

	couponPtrs = s.MapCouponDistributionWithStatEntityToPtr(rows)
	return
}
